<?php
  class Model_search extends CI_Model{
	  protected $_table = "tbl_tours";
	  public function __construct(){
		  parent::__construct();
		  $this->load->database();
	  }
	  public function count_all($destina,$holiday){
		  if($destina == 0 && $holiday != 0){
			  $sql = "select * from tbl_tours where type_id = '$holiday'";
			  //return "1";
		  }elseif($holiday == 0 && $destina != 0){
			  $sql = "select * from tbl_tours where destina_id = '$destina'";
			  //return "2";
		  }elseif($destina == 0 && $holiday == 0){
			  $sql = "select * from tbl_tours";
			  //return "3";
		  }else{
			  $sql = "select * from tbl_tours where destina_id = '$destina' and type_id = '$holiday'";
			  //return "4";
		  }
		  //die();
		  $query = $this->db->query($sql);
		  return $query->num_rows();
	  }
	  public function list_all($destina,$holiday,$off,$start){
		  if($start == NULL){ $start = 0;}
		  if($destina == 0 && $holiday != 0){
			  $sql = "select * from tbl_tours where type_id = '$holiday' limit $start,$off";
		  }elseif($holiday == 0 && $destina != 0){
			  $sql = "select * from tbl_tours where destina_id = '$destina' limit $start,$off";
		  }elseif($destina == 0 && $holiday == 0){
			  $sql = "select * from tbl_tours limit $start,$off";
		  }else{
			  $sql = "select * from tbl_tours where destina_id = '$destina' and type_id = '$holiday' limit $start,$off";
		  }
		  $query = $this->db->query($sql);
		  return $query->result_array();
	  }
      public function search($destina,$holiday,$tours_dura,$off,$start){
          $where = "";
          if($destina ==0 && $holiday ==0 && $tours_dura ==0){
              $where = "tours_status = 1";
          }
          elseif($destina ==0 && $holiday ==0 && $tours_dura !=0){
              $where .= " tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina ==0 && $holiday !=0 && $tours_dura ==0){
              $where .= " type_id = {$holiday} ";
              $where = trim($where);
          }
          elseif($destina ==0 && $holiday !=0 && $tours_dura !=0){
              $where .= " type_id = {$holiday} ";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday ==0 && $tours_dura ==0){
              $where .= "destina_id = {$destina}";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday ==0 && $tours_dura !=0){
              $where .= "destina_id = {$destina}";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday !=0 && $tours_dura ==0){
              $where .= "destina_id = {$destina}";
              $where .= " AND type_id = {$holiday} ";
              $where = trim($where);
          }
          else{
              $where .= "destina_id = {$destina}";
              $where .= " AND type_id = {$holiday} ";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          $data = $this->db
              ->order_by("tours_id", 'desc')
              ->where($where)
              ->where("tours_status",1)
              ->get($this->_table, $off, $start)
              ->result_array();
          return $data;
      }
      public function count_search($destina,$holiday,$tours_dura){
          $where = "";
          if($destina ==0 && $holiday ==0 && $tours_dura ==0){
              $where = "tours_status = 1";
          }
          elseif($destina ==0 && $holiday ==0 && $tours_dura !=0){
              $where .= " tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina ==0 && $holiday !=0 && $tours_dura ==0){
              $where .= " type_id = {$holiday} ";
              $where = trim($where);
          }
          elseif($destina ==0 && $holiday !=0 && $tours_dura !=0){
              $where .= " type_id = {$holiday} ";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday ==0 && $tours_dura ==0){
              $where .= "destina_id = {$destina}";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday ==0 && $tours_dura !=0){
              $where .= "destina_id = {$destina}";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          elseif($destina !=0 && $holiday !=0 && $tours_dura ==0){
              $where .= "destina_id = {$destina}";
              $where .= " AND type_id = {$holiday} ";
              $where = trim($where);
          }
          else{
              $where .= "destina_id = {$destina}";
              $where .= " AND type_id = {$holiday} ";
              $where .= " AND tours_dura = {$tours_dura} ";
              $where = trim($where);
          }
          $where = trim($where);
          return $this->db->where($where)->count_all_results($this->_table);
      }
  }